package com.bcntouch.touch2act.Contribute;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.NavUtils;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.bcntouch.touch2act.R;
import com.bcntouch.touch2act.Contribute.ContributionStorage.ContributionMetaData;

public class ContributeActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
	
	// This is the Adapter being used to display the list's data.
    SimpleCursorAdapter mAdapter;
    
    ListView mListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contribute);
//        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        ListView listView = (ListView) findViewById(R.id.contributions);
        
        mListView = listView;
        
        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new SimpleCursorAdapter(this,
                						   android.R.layout.simple_list_item_2,
                						   null,
                						   new String[] { ContributionMetaData.TEXT, ContributionMetaData.TYPE },
                						   new int[] { android.R.id.text1, android.R.id.text2 },
                						   0);
        
        mListView.setAdapter(mAdapter);
        
        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_contribute, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	
    	mListView.invalidateViews();
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	
    	finish();
    }

	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return ContributionStorage.getAllContributionsCursorLoader(this);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		// Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		// This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
	}

}
